{% extends 'base.html' %}

{% block title %}采购订单详情 - 维修管理系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>采购订单详情</h2>
    <a href="{% url 'spare_parts:purchase_list' %}" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left"></i> 返回列表
    </a>
</div>

<!-- 订单基本信息 -->
<div class="card mb-4">
    <div class="card-header">
        <h5 class="card-title mb-0">订单信息</h5>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <p><strong>订单编号:</strong> {{ purchase_order.order_number }}</p>
                <p><strong>供应商:</strong> {{ purchase_order.supplier.name }}</p>
                <p><strong>状态:</strong>
                    <span class="badge 
                        {% if purchase_order.status == 'draft' %}bg-secondary
                        {% elif purchase_order.status == 'submitted' %}bg-warning
                        {% elif purchase_order.status == 'approved' %}bg-info
                        {% elif purchase_order.status == 'ordered' %}bg-primary
                        {% elif purchase_order.status == 'received' %}bg-success
                        {% elif purchase_order.status == 'cancelled' %}bg-danger
                        {% else %}bg-light text-dark{% endif %}">
                        {{ purchase_order.get_status_display }}
                    </span>
                </p>
                <p><strong>总金额:</strong> ¥{{ purchase_order.total_amount }}</p>
            </div>
            <div class="col-md-6">
                <p><strong>申请人:</strong> {{ purchase_order.requested_by.username }}</p>
                <p><strong>创建时间:</strong> {{ purchase_order.created_at|date:"Y-m-d H:i" }}</p>
                {% if purchase_order.approved_by %}
                <p><strong>审批人:</strong> {{ purchase_order.approved_by.username }}</p>
                <p><strong>审批时间:</strong> {{ purchase_order.approved_at|date:"Y-m-d H:i" }}</p>
                {% endif %}
            </div>
        </div>
        {% if purchase_order.notes %}
        <div class="mt-3">
            <strong>备注:</strong>
            <p class="text-muted">{{ purchase_order.notes }}</p>
        </div>
        {% endif %}
    </div>
</div>

<!-- 订单项列表 -->
<div class="card">
    <div class="card-header">
        <h5 class="card-title mb-0">采购项</h5>
    </div>
    <div class="card-body">
        {% if order_items %}
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>备件名称</th>
                        <th>型号规格</th>
                        <th>数量</th>
                        <th>单价</th>
                        <th>总价</th>
                        <th>已收货数量</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in order_items %}
                    <tr>
                        <td>{{ item.spare_part.name }}</td>
                        <td>{{ item.spare_part.model }}</td>
                        <td>{{ item.quantity }} {{ item.spare_part.unit }}</td>
                        <td>¥{{ item.unit_price }}</td>
                        <td>¥{{ item.total_price }}</td>
                        <td>
                            {{ item.received_quantity }} {{ item.spare_part.unit }}
                            {% if item.is_fully_received %}
                            <span class="badge bg-success ms-2">已完成</span>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="text-center py-4">
            <i class="bi bi-cart-x text-muted" style="font-size: 3rem;"></i>
            <p class="text-muted mt-2">暂无采购项</p>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}
